package cn.ming.wfchat.kit.third.location.viewholder;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import org.json.JSONException;
import org.json.JSONObject;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.kit.R2;
import cn.ming.wfchat.kit.annotation.EnableContextMenu;
import cn.ming.wfchat.kit.annotation.MessageContentType;
import cn.ming.wfchat.kit.conversation.ConversationFragment;
import cn.ming.wfchat.kit.conversation.message.model.UiMessage;
import cn.ming.wfchat.kit.conversation.message.viewholder.NormalMessageContentViewHolder;
import cn.ming.wfchat.kit.user.UserInfoActivity;
import cn.wildfirechat.message.CardMessageContent;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

@MessageContentType({CardMessageContent.class})
@EnableContextMenu
public class CardMessageContentViewHolder extends NormalMessageContentViewHolder {

    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_connect)
    TextView tvConnect;
    @BindView(R2.id.locationLinearLayout)
    LinearLayout locationLinearLayout;
    private UserInfo info;

    public CardMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    public void onBind(UiMessage message) {
        try {
            JSONObject jsonObject = new JSONObject(message.message.content.encode().content);
            JSONObject visitingCard = jsonObject.getJSONObject("visitingCard");
            info = ChatManager.Instance().getUserInfo(visitingCard.optString("userId"), false);
            tvName.setText(info.displayName);
            tvConnect.setText("账号：" + info.mobile);
            Glide.with(fragment).load(info.portrait)
                    .apply(new RequestOptions().centerCrop()).into(ivHead);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R2.id.locationLinearLayout)
    public void onClick(View view) {
        Intent intent = new Intent(fragment.getActivity(), UserInfoActivity.class);
        intent.putExtra("userInfo", info);
        fragment.startActivity(intent);
    }
}
